// 格式化金额
export function formatAmount(amount) {
  return parseFloat(amount).toFixed(2)
}

// 格式化日期
export function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
  const d = new Date(date)
  const year = d.getFullYear()
  const month = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  const hours = String(d.getHours()).padStart(2, '0')
  const minutes = String(d.getMinutes()).padStart(2, '0')
  const seconds = String(d.getSeconds()).padStart(2, '0')

  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds)
}

// 计算佣金
export function calculateCommission(amount, rate) {
  return (amount * rate / 100).toFixed(2)
}

// 获取分销商等级名称
export function getLevelName(level) {
  const levelMap = {
    'bronze': '青铜分销商',
    'silver': '白银分销商',
    'gold': '黄金分销商',
    'diamond': '钻石分销商'
  }
  return levelMap[level] || '普通用户'
}

// 获取分销商等级图标
export function getLevelIcon(level) {
  const iconMap = {
    'bronze': '/static/images/distribution/bronze.png',
    'silver': '/static/images/distribution/silver.png',
    'gold': '/static/images/distribution/gold.png',
    'diamond': '/static/images/distribution/diamond.png'
  }
  return iconMap[level] || '/static/images/distribution/default.png'
}

// 获取佣金状态文本
export function getCommissionStatusText(status) {
  const statusMap = {
    'pending': '待结算',
    'settled': '已结算',
    'withdrawn': '已提现'
  }
  return statusMap[status] || '未知状态'
}

// 获取佣金状态样式
export function getCommissionStatusStyle(status) {
  const styleMap = {
    'pending': {
      color: '#FFA500',
      backgroundColor: '#FFF5E6'
    },
    'settled': {
      color: '#52C41A',
      backgroundColor: '#F6FFED'
    },
    'withdrawn': {
      color: '#999',
      backgroundColor: '#F5F5F5'
    }
  }
  return styleMap[status] || {
    color: '#999',
    backgroundColor: '#F5F5F5'
  }
}

// 获取提现状态文本
export function getWithdrawStatusText(status) {
  const statusMap = {
    'pending': '待审核',
    'approved': '已通过',
    'rejected': '已拒绝',
    'completed': '已完成'
  }
  return statusMap[status] || '未知状态'
}

// 获取提现状态样式
export function getWithdrawStatusStyle(status) {
  const styleMap = {
    'pending': {
      color: '#FFA500',
      backgroundColor: '#FFF5E6'
    },
    'approved': {
      color: '#52C41A',
      backgroundColor: '#F6FFED'
    },
    'rejected': {
      color: '#FF4D4F',
      backgroundColor: '#FFF1F0'
    },
    'completed': {
      color: '#1890FF',
      backgroundColor: '#E6F7FF'
    }
  }
  return styleMap[status] || {
    color: '#999',
    backgroundColor: '#F5F5F5'
  }
}

// 检查是否满足升级条件
export function checkUpgradeConditions(stats, conditions) {
  return {
    totalCommission: stats.totalCommission >= conditions.totalCommission,
    teamCount: stats.teamCount >= conditions.teamCount,
    monthlyOrders: stats.monthlyOrders >= conditions.monthlyOrders
  }
}

// 获取升级进度
export function getUpgradeProgress(stats, conditions) {
  return {
    totalCommission: Math.min(100, (stats.totalCommission / conditions.totalCommission) * 100),
    teamCount: Math.min(100, (stats.teamCount / conditions.teamCount) * 100),
    monthlyOrders: Math.min(100, (stats.monthlyOrders / conditions.monthlyOrders) * 100)
  }
}

// 格式化文件大小
export function formatFileSize(bytes) {
  if (bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

// 检查文件类型
export function checkFileType(file) {
  const imageTypes = ['image/jpeg', 'image/png', 'image/gif']
  const videoTypes = ['video/mp4', 'video/quicktime']
  const documentTypes = ['application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']

  if (imageTypes.includes(file.type)) return 'image'
  if (videoTypes.includes(file.type)) return 'video'
  if (documentTypes.includes(file.type)) return 'document'
  return 'other'
}

// 生成分享链接
export function generateShareLink(userId, productId) {
  return `${window.location.origin}/pages/product/detail?id=${productId}&distributor=${userId}`
}

// 生成分享二维码
export function generateShareQRCode(link) {
  // 这里可以调用后端接口生成二维码
  return `${window.location.origin}/api/qrcode?content=${encodeURIComponent(link)}`
}

// 复制文本到剪贴板
export function copyToClipboard(text) {
  return new Promise((resolve, reject) => {
    if (navigator.clipboard && window.isSecureContext) {
      navigator.clipboard.writeText(text)
        .then(() => resolve(true))
        .catch(() => reject(new Error('复制失败')))
    } else {
      const textArea = document.createElement('textarea')
      textArea.value = text
      textArea.style.position = 'fixed'
      textArea.style.left = '-999999px'
      textArea.style.top = '-999999px'
      document.body.appendChild(textArea)
      textArea.focus()
      textArea.select()
      try {
        document.execCommand('copy')
        textArea.remove()
        resolve(true)
      } catch (error) {
        textArea.remove()
        reject(new Error('复制失败'))
      }
    }
  })
} 